//EFI controller v002
//#include "main_set.h"
#include <main_set.h>
void main(void)
{
    int8    i, dis_swap= 0;
    int16   j;
    int8    *P_fparm, *R_parm;
#if (1 == TRACE)
    int8 deb_cnt = 0;
#endif

//++++++++++++++++++ init hw +++++++++++++++++
    init_hw();
    lcd_init();
    lcd_cursor(1);
    lcd_clear();
    /* 1. row is on 0x00; 2. row is on 0x40; 3. row is on 0x14; 4. row is on 0x54 */
          //'------------------'
    lcd_putch("0123456789abcdef0123");      //first row
    lcd_goto(0x40);                         //second row
    //lcd_putch(0x00);   // blank
    lcd_putch(0x01);   //
    lcd_putch(0x02);   // user symbols
    lcd_putch(0x03);   //
    lcd_putch(0x04);   //
    //'----------'first 4-user symbols
    lcd_putch("Test LCD !!!");
    lcd_putch(0x04);   //
    lcd_putch(0x03);   // user symbols
    lcd_putch(0x02);   //
    lcd_putch(0x01);   //
    lcd_goto(0x14);
              //'------------------'
    lcd_putch("EFI i2c display 18F ");
    lcd_goto(0x54);
          //'------------------'
    lcd_putch("First Fuel Inject!!!");
    beeper(100, 1);
    delay_ms(500);
    memset(&in, 0x00, sizeof(in));
    memset(&out, 0x00, sizeof(out));
//++++++++++++++ Enable all needed interrupts forom hw +++++++++++++++++++
    buffer_in = &in;
    buffer_out = &out;
    enable_all_ints();

// ++++++++++++++++++ MAIN LOOP ++++++++++++++++++++++++++++++++
    
    while(TRUE) {
#ifdef ADC_SYNCH
        if (ad_ok == 1) {
            ad_ok = 0;    //flag will be set when all adc channels are done
        }
#endif
        if (refresh_data) {
            //Delay_ms (10); //only debug sim !!!
            refresh_data = 0;
            //temp_resistor = (((int32)ad_ch[2]*TEMP_BRIGE)/(ad_ch[1]-ad_ch[2]));
            //printf( "ch1: %Lu. ch2: %Lu. res: %Ld.\r\n",ad_ch[1],ad_ch[2],(signed int32)temp_resistor);/

            /* lo oboroti - calculate display bars*/    
            if (in.in.rpm > 600) {
                barob=((in.in.rpm - 530) / 174) / 5;
                barobd=(((in.in.rpm - 530) / 174) - (barob * 5));
            } else {
                barob = 0;
                barobd = 0;
            }
            
            if ((in.bar_percent < 101) && (in.bar_percent > 0)) {
                barv=((in.bar_percent / 2.5) / 5);               
                barvd=((in.bar_percent / 2.5) - (barv * 5));       
            } else {
                barv = barvd = 0;
            }

            if (!in.ign_power_up) {
                bar_p_inj=(int8)(in.in.pw1 / TIME_INJ_SCALE);
            } else {
                // pulse_crank pulsewidths  (ms x 10) bar uses a us
                bar_p_inj=(int8)(((int16)in.pulse_crank * 100) / TIME_INJ_SCALE);
            }

            if ((bar_p_inj < 101) && (bar_p_inj > 0)) {
                    barinj=((bar_p_inj / 2.5) / 5);
                    barinjd=((bar_p_inj / 2.5) - (barinj * 5));
            }else {
                barinj = barinjd = 0;
            }
            
            kb_input();

            if (!freeze_dispaly) {
                if (dis_swap < SWAP_DISPLAY_COUNT) {
                    dis_swap ++;
                } else {
                    dis_swap = 0;
                    if (display_mode < MAX_DISPLAY) {
                        display_mode ++;
                    } else {
                        display_mode = NORMAL;
                    }
                }
            }

//+++++++++++++++++ display selected data ++++++++++++++++++
            switch (display_mode) {
                default:
                case NORMAL:
                    new_dis(MODE_DEF);
                    break;
                case FUEL:
                    new_dis(MODE_FUEL);
                    break;
                case FUEL_MY:
                    new_dis(MODE_FUEL_NEW);
                    break;
                case ADC:
                    display_adc();
                    break;
                case IGN_1:
                case IGN_2:    
                case IGN_3:
                    dispaly_ign_time(display_mode);
                    break;
                case KPA:
                    display_env();
                    break;
            }    

            //set_menu();
        }

//===============burn settings=======================
        if (burn_flag) {
            //disable_interrupts(GLOBAL);
            burn_flag = 0;
            P_fparm = &inpflash;
            R_parm  = &in_parm;
            j = sizeof(in_parm) / FLASH_BLOCK_SIZE;

#ifndef BURN_DISABLE
            for ( i = 0; i < j; i++) {
                write_program_memory (P_fparm,R_parm,FLASH_BLOCK_SIZE);
                P_fparm += FLASH_BLOCK_SIZE;
                R_parm += FLASH_BLOCK_SIZE;
            }

            i = sizeof(in_parm) - (j*FLASH_BLOCK_SIZE);
            write_program_memory (P_fparm,R_parm,i);
#endif
            beeper(50, 2);
            //delay_ms (100);
            //RST=1;
            //enable_interrupts(GLOBAL);
        }

//!!!!!!!!!!! RESET TO RUN BOOT LOADER !!!!!!!!!!!!!
        if(RST) {
            RST=0;
            msg_bootloader();
            INJ_CLOSE
            reset_cpu();
        }
    }
}

